package com.nkzy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.DeptFormDTO;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.pojo.Dept;
import com.nkzy.domain.vo.DeptVO;
import com.nkzy.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RequestMapping("/depts")
@RestController
@CrossOrigin
public class DeptController {

    @Autowired
    private DeptService deptService;

    // 保存部门
    @PostMapping
    @Operation(summary = "保存部门信息")
    public Result saveDept(@RequestBody DeptFormDTO deptFormDTO){
        Dept dept = BeanUtil.copyProperties(deptFormDTO, Dept.class);
        deptService.save(dept);
        return Result.success();
    }

    // 查询一个部门
    @GetMapping("/{id}")
    @Operation(summary = "查询一个部门")
    public Result getDept(@PathVariable Long id){
        Dept dept = deptService.getById(id);
        DeptVO deptVO = BeanUtil.copyProperties(dept, DeptVO.class);
        return Result.success(deptVO);
    }

    // 删除一个部门
    @DeleteMapping("/{id}")
    @Operation(summary = "删除一个部门")
    public Result deleteDept(@PathVariable Long id){
        deptService.removeById(id);
        return Result.success();
    }

    // 根据id的集合查询多个部门
    @GetMapping
    @Operation(summary = "根据id的集合查询多个部门")
    public Result getDepts(@RequestParam("ids") List<Long> ids){
        List<Dept> depts = deptService.listByIds(ids);
        List<DeptVO> deptsVO = BeanUtil.copyToList(depts, DeptVO.class);
        return Result.success(deptsVO);
    }

    // 查询所有部门
    @GetMapping("/all")
    @Operation(summary = "查询所有部门")
    public Result getAllDepts(){
        List<Dept> depts = deptService.list();
        List<DeptVO> deptsVO = BeanUtil.copyToList(depts, DeptVO.class);
        return Result.success(deptsVO);
    }

    // 修改一个部门
    @PutMapping
    @Operation(summary = "修改一个部门")
    public Result updateDept(@RequestBody DeptFormDTO deptFormDTO){
        Dept dept = BeanUtil.copyProperties(deptFormDTO, Dept.class);
        deptService.updateById(dept);
        return Result.success();
    }
}